//
//  HNNewsDetailController.m
//  HappyNews
//
//  Created by Sai on 15/12/28.
//  Copyright © 2015年 bai wang. All rights reserved.
//

#import "HNNewsDetailController.h"
#import "HNNewsDetailCell.h"
#import "HNRequestManager.h"
#import "HNNewsDetailModel.h"
#import "HNNewsWebViewController.h"
#import "HNNewsDetailStories.h"
#import "MJRefresh.h"
@interface HNNewsDetailController ()

@property (nonatomic, strong)NSDictionary *NewsDetailDic;

@property (nonatomic, strong)HNNewsDetailModel *model;

@property (nonatomic, assign) NSInteger requestStrart;

@property (nonatomic, strong) NSMutableArray *storiseArr;
@end

@implementation HNNewsDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    _requestStrart = 0;
    _storiseArr = [NSMutableArray array];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self setUpData];
    [self addtableView];
    
}

- (void)addtableView
{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.rowHeight = 100;
    
    [self.tableView registerClass:[HNNewsDetailCell class] forCellReuseIdentifier:@"NewsDetailCell"];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _model.stories.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 把webView的model取出来
    HNNewsDetailStories *model = _model.stories[indexPath.row];
    NSString *str = [NSString stringWithFormat:@"%ld",model.Id];
    // 把对象装进数组
    [_storiseArr addObject:str];
    HNNewsDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsDetailCell"];
    
    // 解决复用问题
    [cell.NewsDatailCellImageView removeFromSuperview];
    [cell.NewsDatailCellLabel removeFromSuperview];
    cell.model = _model.stories[indexPath.row];
    return cell;
}

// 数据解析
- (void)setUpData
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://news-at.zhihu.com/api/4/theme/%@",self.Id]];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
    [request setHTTPMethod:@"GET"];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        _model = [[HNNewsDetailModel alloc]init];
        [_model setValuesForKeysWithDictionary:dic];
        for (NSDictionary *stoDic in _model.stories ) {
          
          
        }
        
        NSLog(@"%@",_storiseArr);
        [self.tableView reloadData];
    }];
    
    
}

// 点击跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    HNNewsWebViewController *webVC = [[HNNewsWebViewController alloc]init];
    webVC.storiseArr = _storiseArr;
    HNNewsDetailStories *model = _model.stories[indexPath.row];
    webVC.Id = model.Id;
    webVC.DetailId = _Id;
    [self.navigationController pushViewController:webVC animated:YES];
}




/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
